from flask import Blueprint,jsonify,request
from db import db1
import json

users_bp = Blueprint('users',__name__)
def get_data():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    return data

@users_bp.route('/show')
def show():
    sql = 'select * from hahah'
    res = db1.fid_all(sql)
    return jsonify({"code":200,"list":res})

@users_bp.route('/yewu',methods=['POST','GET','PUT','DELETE'])
def yewu():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = '''insert into yewu values(0,'%s','%s') '''%(data['name'],'')
        db1.update(sql)
        return jsonify({"code":200})

    elif request.method == 'GET':
        sql = '''select * from yewu'''
        res = db1.fid_all(sql)
        return jsonify({"code":200,"list":res})

@users_bp.route('/setparams',methods=['POST','GET'])
def setparams():
    if request.method=='POST':
        data = get_data()
        #更新工作流中的参数
        sql= "update yewu set params='%s' where id=%d"%(data['params'],int(data['id']))
        db1.update(sql)
        return jsonify({"code":200})
    else:
        id = request.args.get('wid')
        sql = "select * from yewu where id=%d" % (int(id))
        res = db1.find(sql)
        return jsonify({"code": 200, 'workf': res})
